<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use DB;

class WarehouseMaterials extends Model
{
    protected $table = 'warehouse_materials';
    public static function get_warehouse_type($type=0){
        $data = ['1'=>'新增入库','2'=>'盘点入库','3'=>'归还入库'];
        return $data[$type]??$data;
    }

    //所属单位分类
    public function checkcategory()
    {
        return $this->belongsTo('App\Model\Checkcategory','checkcategory_id');
    }
    //物资分类
    public function goods()
    {
        return $this->belongsTo('App\Model\Goods','goods_id');
    }
    //物资名称
    public function materialList()
    {
        return $this->belongsTo('App\Model\MaterialList','material_id');
    }
    //单位名称
    public function company()
    {
        return $this->belongsTo('App\Model\Company','company_id');
    }
    //申请人
    public function Users()
    {
        return $this->belongsTo('App\Model\Users','user_id');
    }
    public static function getMaterialsNum($goods_id=0,$material_id=0,$checkcategory_id=0,$company_id=0,$classify=0){
        $ins_all = WarehouseMaterials::select(DB::raw('SUM(`number`) as sumnum'))->where('goods_id',$goods_id)->where('material_id',$material_id)->where('checkcategory_id',$checkcategory_id)->where('company_id',$company_id)->first();
        if($classify==3){
            $ins = WarehouseMaterials::select(DB::raw('SUM(`number`) as sumnum'))->where('goods_id',$goods_id)->where('material_id',$material_id)->where('checkcategory_id',$checkcategory_id)->where('company_id',$company_id)->where('warehouse_type',$classify)->first();
        }else{
            $ins = WarehouseMaterials::select(DB::raw('SUM(`number`) as sumnum'))->where('goods_id',$goods_id)->where('material_id',$material_id)->where('checkcategory_id',$checkcategory_id)->where('company_id',$company_id)->whereIn('warehouse_type',[1,2])->first();
        }

        return ['ins_all'=>$ins_all['sumnum']??0,'ins'=>$ins['sumnum']??0];
    }
}
